//
//  RecommondGameView.swift
//  DouYuZB
//
//  Created by James on 2017/10/22.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

private let kGameViewCellId = "kGameViewCellId"
private let kInsetMargin = CGFloat(10)

class RecommondGameView: UIView {
    var anchorGroup : [AnchorGroup]? {
        didSet{
            //移除不需要显示的
            anchorGroup?.remove(at: 0)
            anchorGroup?.remove(at: 0)
            //添加更多
            let more = AnchorGroup(dict: [:])
            more.tag_name = "更多"
            anchorGroup?.append(more)
            collectionView.reloadData()
            
        }
    }
    // MARK: - 控件属性
    
    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        autoresizingMask = .init(rawValue: 0)
        
        collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kGameViewCellId)
        collectionView.contentInset = UIEdgeInsetsMake(0, kInsetMargin, 0, kInsetMargin)
    }
    
    class func recommondGameView() -> RecommondGameView {
        return Bundle.main.loadNibNamed("RecommondGameView", owner: nil, options: nil)?.first as! RecommondGameView
    }

}
extension RecommondGameView:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return anchorGroup?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:kGameViewCellId, for: indexPath) as! CollectionGameCell
        let group = anchorGroup![indexPath.item]
        cell.group = group
        
        return cell
    }
    

    
}
